home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH16 / INFIX2.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-10  |  5.0 KB  |  228 lines

  1. ; INFIX.ASM
  2. ;
  3. ; A simple program which demonstrates the pattern matching routines in the
  4. ; UCR library.  This program accepts an arithmetic expression on the command
  5. ; line (no interleaving spaces in the expression is allowed, that is, there
  6. ; must be only one command line parameter) and converts it from infix notation
  7. ; to postfix (rpn) notation.
  8.  
  9.         .xlist
  10.         include     stdlib.a
  11.         includelib    stdlib.lib
  12.         matchfuncs
  13.         .list
  14.  
  15.  
  16. dseg        segment    para public 'data'
  17.  
  18. ; Grammar for simple infix -> postfix translation operation:
  19. ;
  20. ; E -> FE'
  21. ; E' -> +F {output '+'} E' | -F {output '-'} E' | <empty string>
  22. ; F -> TF'
  23. ; F -> *T {output '*'} F' | /T {output '/'} F' | <empty string>
  24. ; T -> -T {output 'neg'} | S
  25. ; S -> <constant> {output constant} | (E)
  26. ;
  27. ; UCR Standard Library Pattern which handles the grammar above:
  28.  
  29. ; An expression consists of an "E" item followed by the end of the string:
  30.  
  31. infix2rpn    pattern    {sl_Match2,E,,EndOfString}
  32. EndOfString    pattern    {EOS}
  33.  
  34. ; An "E" item consists of an "F" item optionally followed by "+" or "-"
  35. ; and another "E" item:
  36.  
  37. E        pattern    {sl_Match2, F,,Eprime}
  38. Eprime        pattern    {MatchChar, '+', Eprime2, epf}
  39. epf        pattern    {sl_Match2, F,,epPlus}
  40. epPlus        pattern    {OutputPlus,,,Eprime}
  41.  
  42. Eprime2        pattern    {MatchChar, '-', Succeed, emf}
  43. emf        pattern    {sl_Match2, F,,epMinus}
  44. epMinus        pattern    {OutputMinus,,,Eprime}
  45.  
  46. ; An "F" item consists of a "T" item optionally followed  by "*" or "/"
  47. ; followed by another "T" item:
  48.  
  49. F        pattern    {sl_Match2, T,,Fprime}
  50. Fprime        pattern    {MatchChar, '*', Fprime2, fmf}
  51. fmf        pattern    {sl_Match2, T, 0, pMul}
  52. pMul        pattern    {OutputMul,,,Fprime}
  53.  
  54. Fprime2        pattern    {MatchChar, '/', Succeed, fdf}
  55. fdf        pattern    {sl_Match2, T, 0, pDiv}
  56. pDiv        pattern    {OutputDiv, 0, 0,Fprime}
  57.  
  58. ; T item consists of an "S" item or a "-" followed by another "T" item:
  59.  
  60. T        pattern    {MatchChar, '-', S, TT}
  61. TT        pattern    {sl_Match2, T, 0,tpn}
  62. tpn        pattern    {OutputNeg}
  63.  
  64. ; An "S" item is either a string of one or more digits or "(" followed by
  65. ; and "E" item followed by ")":
  66.  
  67. Const        pattern    {sl_Match2, DoDigits, 0, spd}
  68. spd        pattern    {OutputDigits}
  69. DoDigits    pattern    {Anycset, Digits, 0, SpanDigits}
  70. SpanDigits    pattern    {Spancset, Digits}
  71.  
  72. S        pattern    {MatchChar, '(', Const, IntE}
  73. IntE        pattern    {sl_Match2, E, 0, CloseParen}
  74. CloseParen    pattern    {MatchChar, ')'}
  75.  
  76.  
  77. Succeed        pattern    {DoSucceed}
  78.  
  79.  
  80.         include    stdsets.a
  81.  
  82. dseg        ends
  83.  
  84.  
  85.  
  86. cseg        segment    para public 'code'
  87.         assume    cs:cseg, ds:dseg
  88.  
  89. ; DoSucceed matches the empty string.  In other words, it matches anything
  90. ; and always returns success without eating any characters from the input
  91. ; string.
  92.  
  93. DoSucceed    proc    far
  94.         mov    ax, di
  95.         stc
  96.         ret
  97. DoSucceed    endp
  98.  
  99.  
  100. ; OutputPlus is a semantic rule which outputs the "+" operator after the
  101. ; parser sees a valid addition operator in the infix string.
  102.  
  103. OutputPlus    proc    far
  104.         print
  105.         byte    " +",0
  106.         mov    ax, di            ;Required by sl_Match
  107.         stc
  108.         ret
  109. OutputPlus    endp
  110.  
  111.  
  112. ; OutputMinus is a semantic rule which outputs the "-" operator after the
  113. ; parser sees a valid subtraction operator in the infix string.
  114.  
  115. OutputMinus    proc    far
  116.         print
  117.         byte    " -",0
  118.         mov    ax, di            ;Required by sl_Match
  119.         stc
  120.         ret
  121. OutputMinus    endp
  122.  
  123.  
  124. ; OutputMul is a semantic rule which outputs the "*" operator after the
  125. ; parser sees a valid multiplication operator in the infix string.
  126.  
  127. OutputMul    proc    far
  128.         print
  129.         byte    " *",0
  130.         mov    ax, di            ;Required by sl_Match
  131.         stc
  132.         ret
  133. OutputMul    endp
  134.  
  135.  
  136. ; OutputDiv is a semantic rule which outputs the "/" operator after the
  137. ; parser sees a valid division operator in the infix string.
  138.  
  139. OutputDiv    proc    far
  140.         print
  141.         byte    " /",0
  142.         mov    ax, di            ;Required by sl_Match
  143.         stc
  144.         ret
  145. OutputDiv    endp
  146.  
  147.  
  148. ; OutputNeg is a semantic rule which outputs the unary "-" operator after the
  149. ; parser sees a valid negation operator in the infix string.
  150.  
  151. OutputNeg    proc    far
  152.         print
  153.         byte    " neg",0
  154.         mov    ax, di            ;Required by sl_Match
  155.         stc
  156.         ret
  157. OutputNeg    endp
  158.  
  159.  
  160. ; OutputDigits outputs the numeric value when it encounters a legal integer
  161. ; value in the input string.
  162.  
  163. OutputDigits    proc    far
  164.         push    es
  165.         push    di
  166.         mov    al, ' '
  167.         putc
  168.         lesi    const
  169.         patgrab
  170.         puts
  171.         free
  172.         stc
  173.         pop    di
  174.         mov    ax, di
  175.         pop    es
  176.         ret
  177. OutputDigits    endp
  178.  
  179.  
  180.  
  181. ; Okay, here's the main program which fetches the command line parameter
  182. ; and parses it.
  183.  
  184. Main        proc
  185.         mov    ax, dseg
  186.         mov    ds, ax
  187.         mov    es, ax
  188.  
  189.         meminit                ; memory to the heap.
  190.  
  191.  
  192.         print
  193.         byte    "Enter an arithmetic expression: ",0
  194.         getsm
  195.         print
  196.         byte    "Expression in postfix form: ",0
  197.  
  198.         ldxi    infix2rpn
  199.         xor    cx, cx
  200.         match
  201.         jc    Succeeded
  202.  
  203.         print
  204.         byte    "Syntax error",0
  205.  
  206. Succeeded:    putcr
  207.  
  208. Quit:        ExitPgm
  209. Main        endp
  210.  
  211. cseg        ends
  212.  
  213.  
  214.  
  215. ; Allocate a reasonable amount of space for the stack (8k).
  216.  
  217. sseg        segment    para stack 'stack'
  218. stk        db    1024 dup ("stack   ")
  219. sseg        ends
  220.  
  221.  
  222. ; zzzzzzseg must be the last segment that gets loaded into memory!
  223.  
  224. zzzzzzseg    segment    para public 'zzzzzz'
  225. LastBytes    db    16 dup (?)
  226. zzzzzzseg    ends
  227.         end    Main
  228.